/******************************************************************************
 * Copyright (C) 2017 Chengdu Dlhd information industry co. LTD.
 * All Rights Reserved.
 * 本软件为成都顶联互动信息产业有限公司开发研制。未经本公司正式书面同意，其他任何个人、团体不得使用、复制、修改或发布本软件.
 *****************************************************************************/
package net.dgg.dggcloud.service.jrcrm.service.business;

import net.dgg.dggcloud.service.common.base.service.BaseService;
import net.dgg.dggcloud.service.common.exception.BizException;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.jrcrm.dao.cms.CmsDeptSetLogDao;
import net.dgg.dggcloud.service.jrcrm.entity.business.DeptSet;
import net.dgg.dggcloud.service.jrcrm.entity.cms.CmsDeptSetLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


/**
 * @ClassName 〈GroupService>
 * @Description
 * @Author 杜倩
 * @Date 2018/3/28 11:42
 */
@Service
public class DeptSetService extends BaseService {

    @Autowired
    private CmsDeptSetLogDao cmsDeptSetLogDao;

    /**
     * 保存抢单接单
     *
     */
    public void saveOrUpdateDeptSet(Integer type,Integer num,User user) {
        try {
            DeptSet deptSet = null;
            if (null != user.getOrgId() ) {
                List list = this.repository.findByProperty(DeptSet.class, "deptId", user.getOrgId());
                if (null != list && list.size()>0) {
                    deptSet = (DeptSet) list.get(0);
                    if(1==type){
                        if(null != num) {
                            deptSet.setIsOrder(num);
                        }
                    }else if(2==type) {
                        if (null != num) {
                            deptSet.setIsGrabOrder(num);
                        }
                    }else if(3 == type){
                        if (null != num) {
                            deptSet.setIsDuty(num);
                        }
                    }
                } else {
                    deptSet = new DeptSet();
                    deptSet.setId(keyWorker.nextId());
                    deptSet.setCreateUser(user);
                    deptSet.setDeptId(user.getOrgId());
                    if(1==type){
                        if(null != num) {
                            deptSet.setIsOrder(num);
                        }else{
                            deptSet.setIsOrder(0);
                        }
                        deptSet.setIsGrabOrder(0);
                        deptSet.setIsDuty(0);
                    }else if(2==type) {
                        if (null != num) {
                            deptSet.setIsGrabOrder(num);
                        }else{
                            deptSet.setIsGrabOrder(0);
                        }
                        deptSet.setIsOrder(0);
                        deptSet.setIsDuty(0);
                    }else if(3==type){
                        if (null != num) {
                            deptSet.setIsDuty(num);
                        }else{
                            deptSet.setIsDuty(0);
                        }
                        deptSet.setIsOrder(0);
                        deptSet.setIsGrabOrder(0);
                    }
                }
                if(null != deptSet){

                    deptSet.setUpdaterUser(user);
                }
                this.repository.saveOrUpdate(deptSet);

                CmsDeptSetLog cmsDeptSetLog = new CmsDeptSetLog();
                cmsDeptSetLog.setId(keyWorker.nextId());
                cmsDeptSetLog.setCreateUser(user);
                cmsDeptSetLog.setUpdaterUser(user);
                cmsDeptSetLog.setOrgId(user.getOrgId());
                cmsDeptSetLog.setStateType(type);
                cmsDeptSetLog.setState(num);
                this.repository.saveOrUpdate(cmsDeptSetLog);
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("设置失败！");
            throw new BizException("设置失败！",10079);
        }
    }

    public DeptSet getDeptSet(Long deptId){
        DeptSet deptSet = null;
        try{

            if (null != deptId) {
                List list = this.repository.findByProperty(DeptSet.class, "deptId",deptId);
                if(null != list && list.size()>0){
                    deptSet = (DeptSet) list.get(0);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("获取抢单接单信息失败！");
            throw new BizException("获取抢单接单信息失败！",10079);
        }

        return deptSet;
    }


    /**
     * 保存抢单接单
     * type  类型   1 接单  2 抢单
     *
     */
    public void saveOrUpdateDeptSet(Integer type,Integer num,long deptId) {
        try {
            DeptSet deptSet = null;
                List list = this.repository.findByProperty(DeptSet.class, "deptId", deptId);
                if (null != list && list.size()>0) {
                    deptSet = (DeptSet) list.get(0);
                    if(1==type){
                        if(null != num) {
                            deptSet.setIsOrder(num);
                        }
                    }else if(2==type) {
                        if (null != num) {
                            deptSet.setIsGrabOrder(num);
                        }
                    }
                } else {
                    deptSet = new DeptSet();
                    deptSet.setId(keyWorker.nextId());
                    deptSet.setDeptId(deptId);
                    if(1==type){
                        if(null != num) {
                            deptSet.setIsOrder(num);
                        }else{
                            deptSet.setIsOrder(0);
                        }
                        deptSet.setIsGrabOrder(0);
                    }else if(2==type) {
                        if (null != num) {
                            deptSet.setIsGrabOrder(num);
                        }else{
                            deptSet.setIsGrabOrder(0);
                        }
                        deptSet.setIsOrder(0);
                    }
                }
                if(null != deptSet){
                }
                this.repository.saveOrUpdate(deptSet);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("添加或修改抢单接单失败！");
            throw new BizException("添加或修改抢单接单失败！",10079);
        }
    }


    public Integer countDeptSetLogByMap(Map map){
        return cmsDeptSetLogDao.countDeptSetLogByMap(map);
    }

    public List<Map> listDeptSetLogByMap(Map map){
        return cmsDeptSetLogDao.listDeptSetLogByMap(map);
    }



}
